package com.tuanzi.loan.web.bean.system;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.tuanzi.loan.business.entity.system.Role;
import com.tuanzi.loan.business.service.system.RoleService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.web.configuration.JSFInfoBean;

import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * @author Ray 2017/09/21
 */
@Data
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class RoleInfoBean extends JSFInfoBean<Role> {

    private static final long serialVersionUID = -2152778554108785342L;

    @Autowired
    private RoleService roleService;

    @PostConstruct
    private void init() {
        String id = getParamFromRequest("id");
        if (StringUtils.isNotBlank(id)) {
            $ = roleService.findOne(id);
        } else {
            $ = new Role();
            changeStatusToAdd();
        }
    }

    public void onSave() {
        $ = roleService.saveOrUpdate($);
        changeStatusToView();
        addSuccessfullyMessage();
    }

}
